import datetime
import os

import requests
from openai import OpenAI

from src.day18.set_env import read_yaml

dic = read_yaml("conf.yaml")
# 初始化OpenAI对象
client = OpenAI(base_url=dic.get("OPENAI_BASE_URL"), api_key=dic.get("OPENAI_API_KEY"))


def generate_image_path():
    # 生成图片路径
    return os.path.join("img_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".png")


def test_image_normal():
    # 文生图，指定模型，给出提示语和大小限制
    response = client.images.generate(
        model="dall-e-3",
        prompt="一只猫在窗户边睡觉",
        size="1024x1024",
    )
    # 得到生成的图片链接
    image_url = response.data[0].url
    print(image_url)
    # 下载并保存图像
    image_response = requests.get(image_url)
    with open(generate_image_path(), 'wb') as f:
        f.write(image_response.content)
